package com.rover.crm.controller;

import com.grid.DataGrid;
import com.grid.Page;
import com.rover.system.controller.BaseController;
import com.rover.crm.entity.BusinessOpportunities;
import com.rover.crm.entity.SalesOfProducts;
import com.rover.inventory.entity.Product;
import com.rover.crm.service.BusinessOpportunitiesService;
import com.rover.crm.service.SalesOfProductsService;
import com.rover.inventory.service.ProductService;
import com.utils.JsonUtil;
import com.utils.HqlGenerateUtil;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 *销售产品
 */
@Controller
@RequestMapping("/salesOfProductsController")
public class SalesOfProductsController extends BaseController {

    @Autowired
    private SalesOfProductsService service;

    @Autowired
    private  BusinessOpportunitiesService  businessOpportunitiesService;
    
    @Autowired
    private   ProductService productService;
    // 用户管理
    @RequestMapping("/query")
    @ResponseBody
    public Map<String, Object> query(HttpServletRequest request, Page page,
                                     HttpServletResponse response) {
        Map<String, Object> map =null;
        try {
            HqlGenerateUtil.installHql(request, SalesOfProducts.class, page);
            DataGrid dataGrid = service.query(page);
            map = JsonUtil.JSONString(dataGrid.getResult(),
                    dataGrid.getTotalCount());
        } catch (Exception e) {
            map = new HashMap<String, Object>();
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> save(SalesOfProducts salesOfProducts, HttpServletRequest request)
           {
        Map<String, Object> map = new HashMap<String, Object>();
        Boolean success = true;
        String message = "";
        try {
        	 if(StringUtils.isBlank(request.getParameter("productId"))){
        		 message="产品没找到";
        		 success=false;
        	 }
        	 if(StringUtils.isBlank(request.getParameter("businessOpportunitiesId"))){
        		 message="业务名称没找到";
        		 success=false;
        	 }
            if(success){
            	salesOfProducts.setBusinessOpportunities(businessOpportunitiesService.<BusinessOpportunities>getEntity(BusinessOpportunities.class,Integer.valueOf( request.getParameter("businessOpportunitiesId"))));
            	salesOfProducts.setProduct(productService.<Product>getEntity(Product.class,Integer.valueOf( request.getParameter("productId"))));
                service.saveOrUpdate(salesOfProducts);
               }
            map.put("success", success);
            map.put("message", message);
        } catch (Exception e) {
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public
    @ResponseBody
    Map<String, Object> delete(@RequestParam("id") String id) {
        Map<String, Object> map = new HashMap<String, Object>();
        // String id = request.getParameter("id");
        try {
            service.delete(SalesOfProducts.class, Integer.valueOf(id));
            map.put("success", true);
            map.put("message", "");

        } catch (Exception e) {
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }



}
